home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Controls
/
Visual Basic Controls.iso
/
vbcontrol
/
xceedzip
/
MEMORY C.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1999-04-26
|
5KB
|
172 lines
// Memory Compression.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "zipDispIds.h" // DISPIDs for methods, properties and events
// The following line imports the type library from XceedZip.dll, and creates
// wrapper classes for the controls (in XceedZip.tlh and XceedZip.tli).
#import "XceedZip.dll" no_namespace named_guids
// Change these defines to customize the sample
#define FILE_TO_ZIP _T( "c:\\test.txt" )
#define ZIP_FILENAME _T( "c:\\test.zip" )
// This identifies the our sink object (it could be any id...)
#define DISPID_ZIP_SINK 1
// Usually, a sink object derives from IDispEventImpl, which uses type info from the
// event source's typelib to get the funcinfo. But IDispEventImpl::GetUserDefinedType()
// only handles TKIND_ALIAS. In many of our events, we have TKIND_ENUM parameters.
// This is why the sink object derives from IDispEventSimpleImpl, which does not use
// type information. We need to provide a _ATL_FUNC_INFO for each event we want to
// handle.
static _ATL_FUNC_INFO QueryMemoryFile_Info =
{
CC_STDCALL, // Calling convention.
VT_EMPTY, // Return type.
10, // Number of arguments.
{ VT_I4 | VT_BYREF, VT_BSTR | VT_BYREF, VT_BSTR | VT_BYREF, VT_I4 | VT_BYREF,
VT_DATE | VT_BYREF, VT_DATE | VT_BYREF, VT_DATE | VT_BYREF,
VT_BOOL | VT_BYREF, VT_BSTR | VT_BYREF, VT_BOOL | VT_BYREF } // Argument types
};
static _ATL_FUNC_INFO ZippingMemoryFile_Info =
{
CC_STDCALL, // Calling convention.
VT_EMPTY, // Return type.
4, // Number of arguments.
{ VT_I4, VT_BSTR, VT_VARIANT | VT_BYREF, VT_BOOL | VT_BYREF } // Argument types
};
// The class CZipEventSink is the event sink.
class CZipEventSink: public IDispEventSimpleImpl< DISPID_ZIP_SINK, CZipEventSink, &DIID__IXceedZipEvents >
{
public:
void _stdcall QueryMemoryFile (
long * lUserTag,
BSTR * sFilename,
BSTR * sComment,
long * lAttributes,
DATE * dtLastModified,
DATE * dtLastAccessed,
DATE * dtCreated,
VARIANT_BOOL * bEncrypted,
BSTR * sPassword,
VARIANT_BOOL * bFileProvided )
{
if( m_bMustProvideFile )
{
m_bMustProvideFile = false;
// Must realloc the provided pointers!
SysReAllocString( sFilename, L"test.dat" );
*bFileProvided = VARIANT_TRUE;
}
else
{
*bFileProvided = VARIANT_FALSE;
}
}
void _stdcall ZippingMemoryFile (
long lUserTag,
BSTR sFilename,
VARIANT * vaDataToCompress,
VARIANT_BOOL * bEndOfData )
{
HANDLE hFile = CreateFile( FILE_TO_ZIP, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL );
if( hFile != INVALID_HANDLE_VALUE )
{
BY_HANDLE_FILE_INFORMATION xFileInfo = { 0 };
GetFileInformationByHandle( hFile, &xFileInfo );
// We create a safearray that will contain the data to be zipped
SAFEARRAY* psa = SafeArrayCreateVector( VT_I1, 0, xFileInfo.nFileSizeLow );
BYTE* pcData;
DWORD dwBytesRead;
SafeArrayAccessData( psa, ( void** )&pcData );
ReadFile( hFile, pcData, xFileInfo.nFileSizeLow, &dwBytesRead, NULL );
SafeArrayUnaccessData( psa );
CloseHandle( hFile );
// We set the variant vaDataToCompress to the safearray we just created.
vaDataToCompress->vt = VT_UI1 | VT_ARRAY;
vaDataToCompress->parray = psa;
}
*bEndOfData = VARIANT_TRUE;
}
// The sink map is used by IDispEventSimpleImpl<>. The map contains information about
// each event handler we want to provide.
BEGIN_SINK_MAP( CZipEventSink )
SINK_ENTRY_INFO(DISPID_ZIP_SINK, DIID__IXceedZipEvents, XCD_ZIP_DISPID_QUERYMEMORYFILE, QueryMemoryFile, &QueryMemoryFile_Info)
SINK_ENTRY_INFO(DISPID_ZIP_SINK, DIID__IXceedZipEvents, XCD_ZIP_DISPID_ZIPPINGMEMORYFILE, ZippingMemoryFile, &ZippingMemoryFile_Info)
END_SINK_MAP()
CZipEventSink( void )
{
m_bMustProvideFile = true;
}
private:
bool m_bMustProvideFile;
};
int main(int argc, char* argv[])
{
CoInitialize( NULL );
try
{
IXceedZipPtr pZip( CLSID_XceedZip ); // Instanciates XceedZip
CZipEventSink xEventSink; // Instanciates the event sink
xEventSink.DispEventAdvise( pZip ); // Connects the event sink to XceedZip
DeleteFile( ZIP_FILENAME );
pZip->ZipFilename = ZIP_FILENAME;
pZip->Zip();
xEventSink.DispEventUnadvise( pZip );
}
catch( const _com_error& xErr )
{
// The generated wrapper classes throw _com_error exceptions when a COM error
// occurs.
printf( "\nCOM Error 0x%08X ( %s ).\n", xErr.Error(), xErr.ErrorMessage() );
}
catch ( ... )
{
printf( "\nUnhandled Exception.\n" );
}
CoUninitialize();
printf( "\nPress any key to quit...\n" );
getch();
return 0;
}